package com.example.base.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    public static Date getDateByLong(Long time){
        Instant instant = Instant.ofEpochMilli(time);
        // 将Instant对象转换为LocalDateTime对象，可以选择指定时区
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        // 格式化日期输出
        // 将LocalDateTime转换为Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return date;
    }


    public static Long getNow(){
        LocalDateTime now = LocalDateTime.now();
        return now.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    public static Long oneMonthAfter(){
        LocalDateTime now = LocalDateTime.now();
        // 获取一个月后的时间
        LocalDateTime oneMonthLater = now.plusMonths(1);
        return oneMonthLater.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    public static List<String> getPast15Days() {
        List<String> past15Days = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 0; i < 15; i++) {
            past15Days.add(currentDate.minusDays(i).format(formatter));
        }
        return past15Days;
    }

    public static Date getDate30MinuteAfter(){
        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();

        // 将当前时间加上半个小时
        calendar.add(Calendar.MINUTE, 30);
        Date halfHourLater = calendar.getTime();
        return halfHourLater;

    }

    public static void main(String[] args) {
        List<String> strings=getPast15Days();
        strings.forEach(e->{
            System.out.println(e);
        });
    }
}
